package com.wdm.command;

import com.wdm.model.Alumni;
import com.wdm.model.AlumniBackup;
import com.wdm.model.AlumniMongo;
import com.wdm.model.AlumniRedis;
import com.wdm.repository.AlumniBackupRepository;
import com.wdm.repository.AlumniMongoRepository;
import com.wdm.repository.AlumniRedisRepository;
import com.wdm.repository.AlumniRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

import java.util.List;

/**
 * Created by Administrator on 2016-5-4.
 */
@Component
public class ExperimentBean implements CommandLineRunner
{
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private AlumniRepository alumniRepository;
    @Autowired
    private AlumniBackupRepository alumniBackupRepository;
    @Autowired
    private AlumniMongoRepository alumniMongoRepository;
    @Autowired
   private AlumniRedisRepository alumniRedisRepository;
    @Override
    public void run(String... strings) throws Exception
    {
        log.info("Experiment begin===============================================");
            //mysqlFindByName();
            //createAlumniRepository();
            // createMongoRepository();
            //mongoFindByName();
            //cretateRedisRepository();
             findRedisByName();
        log.info("Experiment end ================================================");

    }
    public void mysqlFindByName()
    {
        StopWatch sw=new StopWatch();
        sw.start("mysqlFindByName1");
        List<Alumni> alumni=this.alumniRepository.findAllByName("王泽宽");
        sw.stop();
        sw.start("mysqlFindByName2");
        alumni=this.alumniRepository.findAllByName("李飞");
        sw.stop();
        sw.start("mysqlFindByName3");
        alumni=this.alumniRepository.findAllByName("王泽宽");
        sw.stop();
        sw.start("mysqlFindByName4");
        alumni=this.alumniRepository.findAllByName("李飞");
        sw.stop();
        log.info(sw.prettyPrint());

    }
    public void createAlumniRepository()
    {

        Page<Alumni> alumnis=this.alumniRepository.findAll(new PageRequest(0,1000));
        this.alumniBackupRepository.deleteAll();
        int pageCount=alumnis.getTotalPages();
        StopWatch sw=new StopWatch();
        sw.start("MariaDB 10 insert 1000 Record!");
        for(Alumni a:alumnis)this.alumniBackupRepository.save(new AlumniBackup(a));
        sw.stop();
        for(int i=1;i<pageCount;i++)
        {
            alumnis=this.alumniRepository.findAll(new PageRequest(i,1000));
            sw.start("MariaDB 10 insert 1000 Record!");
            for(Alumni a:alumnis)this.alumniBackupRepository.save(new AlumniBackup(a));
            sw.stop();
        }
        log.info(sw.prettyPrint());

    }
    public void createMongoRepository()
    {
        Page<Alumni> alumnis=this.alumniRepository.findAll(new PageRequest(0,1000));
        this.alumniMongoRepository.deleteAll();
        int pageCount=alumnis.getTotalPages();
        StopWatch sw=new StopWatch();
        sw.start("Mongo 3.2 insert 1000 Record!");
        for(Alumni a:alumnis)this.alumniMongoRepository.save(new AlumniMongo(a));
        sw.stop();

        for(int i=1;i<pageCount;i++)
        {
            alumnis=this.alumniRepository.findAll(new PageRequest(i,1000));
            sw.start("Mongo 3.2 insert 1000 Record!");
            for(Alumni a:alumnis)this.alumniMongoRepository.save(new AlumniMongo(a));
            sw.stop();
        }

        log.info(sw.prettyPrint());

    }

    public void mongoFindByName()
    {
        StopWatch sw=new StopWatch();
        sw.start("mongoFindByName1");
        List<AlumniMongo> alumni=this.alumniMongoRepository.findAllByName("王泽宽");
        sw.stop();
        sw.start("mongoFindByName2");
        alumni=this.alumniMongoRepository.findAllByName("李飞");
        sw.stop();
        sw.start("mongoFindByName3");
        alumni=this.alumniMongoRepository.findAllByName("王泽宽");
        sw.stop();
        sw.start("mongoFindByName4");
        alumni=this.alumniMongoRepository.findAllByName("李飞");
        //log.info("lifei is "+alumni.size());
        sw.stop();
        log.info(sw.prettyPrint());

    }
    public void cretateRedisRepository()
    {
        Page<Alumni> alumnis=this.alumniRepository.findAll(new PageRequest(0,5));
        this.alumniRedisRepository.deleteAll();
        int pageCount=alumnis.getTotalPages();
        StopWatch sw=new StopWatch();
        sw.start("Redis 3.0 insert 1000 Record!");
        for(Alumni a:alumnis)this.alumniRedisRepository.save(new AlumniRedis(a));

        sw.stop();

//        for(int i=1;i<pageCount;i++)
//        {
//            alumnis=this.alumniRepository.findAll(new PageRequest(i,1000));
//            sw.start("Redis 3.0 insert 1000 Record!");
//            for(Alumni a:alumnis)this.alumniRedisRepository.save(new AlumniRedis(a));
//            sw.stop();
//        }

        log.info(sw.prettyPrint());
    }
    public void findRedisByName()
    {
        StopWatch sw=new StopWatch();
        sw.start("RedisFindByName1");
        List<AlumniRedis> alumni= (List<AlumniRedis>) this.alumniRedisRepository.findAllByName("魏锦容");

        sw.stop();
        sw.start("RedisFindByName2");
        alumni=this.alumniRedisRepository.findAllByName("唐海杰");
        sw.stop();
        sw.start("RedisFindByName3");
        alumni=this.alumniRedisRepository.findAllByName("蒋凤佼");
        sw.stop();
        sw.start("RedisFindByName4");
        alumni=this.alumniRedisRepository.findAllByName("殷丽");
        sw.stop();

        log.info(sw.prettyPrint());


    }
}
